package com.lanou.dllo.mynovel.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.dllo.mynovel.R;
import com.lanou.dllo.mynovel.adapter.StoreClassifiedAdapter;
import com.lanou.dllo.mynovel.bean.StoreClassified;
import com.lanou.dllo.mynovel.bean.StoreRanking;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/28.
 */
public class StoreClassifiedFragment extends Fragment {

    private RecyclerView recyclerView;
    private StoreClassifiedAdapter adapter;
    private RequestQueue queue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_storeclassified, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 初始化数据
        recyclerView = (RecyclerView) getActivity().findViewById(R.id.storeclassified_recyclerView);
        // 设置布局管理器
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(manager);
        // 初始化适配器
        adapter = new StoreClassifiedAdapter(getActivity());
        // 绑定适配器
        recyclerView.setAdapter(adapter);

        initData();
    }

    // 初始化队列请求
    private void initData() {
        queue = Volley.newRequestQueue(getActivity());
        String url = "http://app.u17.com/v3/app/android/phone/sort/list?sortVersion=1&t=1445584800&v" +
                "=2180001&android_id=653fd641c8c0beb&key=null&come_from=ANZHUO&model" +
                "=Motorola+Moto+X+-+4.3+-+API+18+-+720x1280";
        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        StoreClassified storeClassified = gson.fromJson(
                                response, StoreClassified.class);
                        if (storeClassified != null) {
                            StoreClassified.DataEntity entities = storeClassified.getData();
                            StoreClassified.DataEntity.ReturnDataEntity
                                    entities1 = entities.getReturnData();
                            List<StoreClassified.DataEntity.ReturnDataEntity.RankinglistEntity>
                                    entities2 = entities1.getRankinglist();
                            if (entities2 != null && entities2.size() > 0) {
                                // 将获取到得集合设置到适配器中
                                adapter.addData(entities2);

                            }
                        }
                    }
                },
                new Response.ErrorListener(){

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        /** 打印错误信息 **/
                        error.printStackTrace();
                    }
                }){
            @Override
            public Map<String, String> getHeaders()
                    throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        // 将请求加入到请求队列中
        queue.add(request);
    }
}
